package com.yisu.auction.manager.website.controller.systemNotice;

import com.yisu.auction.ewf.common.enums.ResponseCode;
import com.yisu.auction.ewf.common.web.EasyUIGrid;
import com.yisu.auction.ewf.common.web.JsonResult;
import com.yisu.auction.ewf.entity.SystemNotice;
import com.yisu.auction.manager.core.service.ISystemNoticeService;
import com.yisu.auction.manager.domain.request.SystemNoticePackage.SystemNoticeRequest;
import com.yisu.auction.manager.website.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * author: 黄萧林
 * Business Description：
 * createTime：2017/8/31
 */
@Controller
@RequestMapping(value = "/systemNotice")
public class systemNoticeController extends BaseController {

    @Resource
    private ISystemNoticeService iSystemNoticeService;

    /**
     * 分页查询公告信息
     *
     * @param model
     * @param systemNoticeRequest
     * @return
     */
    @RequestMapping(value = "/index.htm")
    public String index(Model model, SystemNoticeRequest systemNoticeRequest) {
        EasyUIGrid<SystemNotice> systemNoticeEasyUIGrid = iSystemNoticeService.getSystemNoticeInfoByPage(systemNoticeRequest);
        model.addAttribute("systemNoticeVos", systemNoticeEasyUIGrid.getRows());
        model.addAttribute("currentPage", systemNoticeRequest.getPage());
        model.addAttribute("totalPages", (systemNoticeRequest.getTotalRecords() / systemNoticeRequest.getRows()) + 1);
        model.addAttribute("keyword", systemNoticeRequest.getKeyword());
        return "/system/systemNotice/index";
    }

    /**
     * 删除信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete.json")
    @ResponseBody
    public JsonResult delete(Long id) {
        try {
            if (iSystemNoticeService.deleteSystemNoticeInfo(id)) {
                return new JsonResult();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult(ResponseCode.FAILURE.code, "网络君泡妞去了！");
    }


    /**
     * author:黄萧林
     * Business Description:进入修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/modify.htm")
    public String modify(Long id, Model model) {
        try {
            SystemNotice systemNotice = iSystemNoticeService.getSystemNotice(id);
            model.addAttribute("systemNotice", systemNotice);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/system/systemNotice/modify";
    }

    /**
     * author:黄萧林
     * Business Description:修改信息
     *
     * @param systemNotice
     * @return
     */
    @RequestMapping(value = "/modifyMessage.json")
    @ResponseBody
    public JsonResult modifyMessage(SystemNotice systemNotice) {
        try {
            if (iSystemNoticeService.modifySystemNoticeInfo(systemNotice)) {
                return new JsonResult();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult(ResponseCode.FAILURE.code, "网络君泡妞去了！");
    }
}
